//
//  WBWelcomeViewController.swift
//  SinaWeibo_Swift
//
//  Created by TianZhen on 16/4/11.
//  Copyright © 2016年 TianZhen. All rights reserved.
//

import UIKit

class WBWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        if let avatar_large = WBUserAccountViewModel.sharedInstance.userAccount?.avatar_large
        {
            // 显示沙盒保存的头像
            iconImageView.setImageWithURL(NSURL(string: avatar_large)!, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        WBUserAccountViewModel.sharedInstance.loadUserInfo { (error) -> () in
            if let avatar_large = WBUserAccountViewModel.sharedInstance.userAccount?.avatar_large
            {
                // 显示沙盒保存的头像
                self.iconImageView.setImageWithURL(NSURL(string: avatar_large)!, placeholderImage: UIImage(named: "avatar_default_big"))
            }
        }
    }
    
    // 懒加载图片
    private lazy var backgroundImageView: UIImageView =
    {
        let imageView = UIImageView(image: UIImage(named: "ad_background"))
        return imageView
    }()
    
    // 用户头像
    private lazy var iconImageView: UIImageView =
    {
        let iconImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconImageView.layer.cornerRadius = 42.5
        iconImageView.layer.masksToBounds = true
            
        return iconImageView
    }()
    
    private lazy var namelabel: UILabel =
    {
        let namelabel = UILabel()
        namelabel.text = "欢迎归来"
        // 自适应
        namelabel.sizeToFit()
        
        return namelabel
    }()
    
    
    // 头像动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        moveAnimation()
    }
    private func moveAnimation()
    {
        // 当使用自动约束时,不要去修改view的frame bounds center, 修改约束
        iconBottomCons?.constant = -UIScreen.mainScreen().bounds.height - iconBottomCons!.constant
        // 动画, 使用弹簧效果
        // usingSpringWithDamping: 范围 0 - 1之间, 数值越小弹簧效果月明显
        // initialSpringVelocity: 初始速度.
        UIView.animateWithDuration(1.2, delay: 0.1, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                // 移动动画完成, 是label.alpha = 1
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.namelabel.alpha = 1
                    }, completion: { (_) -> Void in
                        // TODO: 回到首页
                        print("动画完成")
                        
                        (UIApplication.sharedApplication().delegate as? AppDelegate)?.switchViewController(true)

                })
        }
    }
    var iconBottomCons: NSLayoutConstraint?
    
    // 添加UI 
    private func setupUI()
    {
        // 添加子控件
        view.addSubview(backgroundImageView)
        view.addSubview(iconImageView)
        view.addSubview(namelabel)
        
        // 添加约束
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        iconImageView.translatesAutoresizingMaskIntoConstraints = false
        namelabel.translatesAutoresizingMaskIntoConstraints = false
        
        // 背景图
        view.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))

        // 头像
        // 宽
        view.addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        // 高
        view.addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        // 底部  view的-160
        iconBottomCons = NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        // 中心点
        let iconX = NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
        view.addConstraint(iconBottomCons!)
        view.addConstraint(iconX)
        
        // label
        view.addConstraint(NSLayoutConstraint(item: namelabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: namelabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
    }
}
